<template>
    <div class="app-container calendar-list-container">

        <el-row class="panel-group" :gutter="40">
      <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
      <div class="card-panel" >
        <div class="card-panel-icon-wrapper icon-money">
          <svg-icon icon-class="money" class-name="card-panel-icon" />
        </div>
        <div class="card-panel-description">
          <div class="card-panel-text">订单总金额</div>
          <count-to class="card-panel-num" :startVal="0" :endVal="dataTotal.count1" :duration="3200"></count-to>
        </div>
      </div>
    </el-col>
    <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
      <div class="card-panel" >
        <div class="card-panel-icon-wrapper icon-money">
          <svg-icon icon-class="money" class-name="card-panel-icon" />
        </div>
        <div class="card-panel-description">
          <div class="card-panel-text">提现手续费</div>
          <count-to class="card-panel-num" :startVal="0" :endVal="dataTotal.count3" :duration="3200"></count-to>
        </div>
      </div>
    </el-col>
    <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col" v-if="merchantId">
      <div class="card-panel" >
        <div class="card-panel-icon-wrapper icon-money">
          <svg-icon icon-class="money" class-name="card-panel-icon" />
        </div>
        <div class="card-panel-description">
          <div class="card-panel-text">可提现金额: {{valAmt}}</div>
          <el-button v-if="withdrawManager_btn_do" type="primary" size="small" @click="handleWithdraw()">提现</el-button>
        </div>
      </div>
    </el-col>
    
    <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
      <div class="card-panel" >
        <div class="card-panel-icon-wrapper icon-money">
          <svg-icon icon-class="money" class-name="card-panel-icon" />
        </div>
        <div class="card-panel-description">
          <div class="card-panel-text">今日提现金额</div>
          <count-to class="card-panel-num" :startVal="0" :endVal="dataTotal.count4" :duration="3200"></count-to>
        </div>
      </div>
    </el-col>
  </el-row>

        <!-- 查询和其他操作 -->
        <div class="filter-container">
            <el-input clearable v-if="merchantId == null"  style="width: 200px;" placeholder="商户号" v-model="listQuery.merchantId"></el-input>
            <el-input clearable  style="width: 200px;" placeholder="订单号" v-model="listQuery.orderId"></el-input>
            <el-select v-model="listQuery.txStatus" placeholder="订单状态" clearable="">
              <el-option label="成功" value="00"> </el-option>
              <el-option label="失败" value="01"> </el-option>
              <el-option label="处理中" value="02"> </el-option>
              <el-option label="提现申请成功" value="03"> </el-option>
            </el-select>
            <el-button class="filter-item" type="primary" v-waves icon="el-icon-search" @click="handleFilter">查询</el-button>
        </div> 
        <!-- 查询结果 -->
        <el-table size="small" :data="list" v-loading="listLoading" element-loading-text="正在查询中。。。" border fit highlight-current-row>
            <el-table-column align="center" min-width="120px" label="平台编号" prop="platformId"></el-table-column>
            <el-table-column align="center" min-width="120px" label="提现订单号" prop="orderId"></el-table-column>
            <el-table-column align="center" min-width="125px" label="外部订单号" prop="outOrderNo"></el-table-column>
            <el-table-column align="center" min-width="80px" label="提现金额" prop="amount"></el-table-column>
            <el-table-column align="center" min-width="90px" label="提现手续费" prop="fee"></el-table-column>
            <!-- <el-table-column align="center" min-width="100px" label="银行编码" prop="bankCode"></el-table-column> -->
            <el-table-column align="center" min-width="110px" label="银行卡号" prop="bankAccountNo"></el-table-column>
            <el-table-column align="center" min-width="90px" label="银行账户名" prop="bankAccountName"></el-table-column>
            <el-table-column align="center" min-width="100px" label="支付宝账号" prop="alipayNo"></el-table-column>
            <!-- <el-table-column align="center" min-width="100px" label="银行联行号" prop="bankUnionCode"></el-table-column> -->
            <el-table-column align="center" min-width="130px" label="提现时间" prop="createTime"></el-table-column>
            <!-- <el-table-column align="center" min-width="100px" label="客户账号" prop="customerId"></el-table-column> -->
            <el-table-column align="center" min-width="110px" label="商户账号" prop="merchantId"></el-table-column>
            <el-table-column align="center" min-width="100px" label="提现状态">
                <template slot-scope="scope">
                    <span v-if="scope.row.txStatus == '00'">成功</span>
                    <span v-if="scope.row.txStatus == '01'">失败</span>
                    <span v-if="scope.row.txStatus == '02'">处理中</span>
                    <span v-if="scope.row.txStatus == '03'">提现申请成功</span>
                </template>
            </el-table-column>
            <el-table-column align="center" min-width="100px" label="备注" prop="summary"></el-table-column>
            <el-table-column align="center" label="操作" width="100" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button v-if="scope.row.txStatus == '03' && withdrawManager_btn_upt" type="primary" size="small" @click="handleUpdate(scope.row)">已提现</el-button>
                </template>
            </el-table-column>

        </el-table>
        <!-- 分页 -->
        <div class="pagination-container" v-show="!listLoading">
            <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="listQuery.page"
                :page-sizes="[10,20,30,50]" :page-size="listQuery.limit" layout="total, sizes, prev, pager, next, jumper" :total="total">
            </el-pagination>
        </div>


        <!-- 提现弹框 -->
        <el-dialog title="提现" :visible.sync="dialogWithdraw">
            <el-form :rules="rule1" ref="withdrawForm" :model="withdrawForm" status-icon label-position="left" label-width="150px" style='width: 400px; margin-left:50px;'>
                <el-form-item label="可提现余额: " prop="valAmt">
                    <el-input v-model="withdrawForm.valAmt" disabled=""></el-input>
                </el-form-item>
                <el-form-item label="提现金额: " prop="payAmt">
                    <el-input v-model="withdrawForm.payAmt"></el-input>
                </el-form-item>
                <el-form-item label="银行卡: " prop="method">
                <el-select v-model="withdrawForm.bankStr" placeholder="请选择" clearable="">
                    <el-option v-for="item in bankList" :key="item.bankNo" :label="item.bankName+' '+item.bankNo" :value="item.bankName+'&'+item.bankAccountName+'&'+item.bankNo">
                    </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="交易密码: " prop="password">
                <el-input v-model="withdrawForm.password"></el-input>
              </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogAddCallBack">取消</el-button>
                <el-button type="primary" @click="sureWithdraw('withdrawForm')">确定</el-button>
            </div>
        </el-dialog>

    </div>
</template>

<style>

</style>

<script>
import { payForAnotherOrderList, updateTxStatus, dataCount, valiableAmt } from '@/api/pay/order/payForAnotherOrder'
import { pageAll } from '@/api/crm/payForAnotherBank/index'
import { doWithdraw } from '@/api/crm/customers/merchantBase'
import waves from '@/directive/waves' // 水波纹指令
import { mapGetters } from 'vuex'
import CountTo from 'vue-count-to'

export default {
  components: {
    CountTo
  },

  directives: { waves },
  data: function() {
    return {
      listQuery: {
        page: 1,
        limit: 10,
        merchantId: '',
        platformId: '',
        orderId: '',
        txStatus: ''
      },
      withdrawManager_btn_upt: false,
      withdrawManager_btn_do: false,
      dialogWithdraw: false,
      list: [],
      total: 0,
      dataTotal: {
        count1: 0,
        count2: 0,
        count3: 0,
        count4: 0
      },
      valAmt: 0,
      bankQuery: {
        page: 0,
        limit: 99,
        platformId: '',
        merchantId: ''
      },
      bankList: [],
      listLoading: false,
      withdrawForm: {
        platformId: '',
        merchantId: '',
        payAmt: '',
        bankStr: '',
        bankName: '',
        bankAccountName: '',
        bankNo: '',
        valAmt: '',
        payChannelNo: '',
        amount: '',
        password: ''
      },
      rule1: {
        payAmt: [
          {
            required: true,
            message: '不能为空',
            trigger: 'blur'
          },
          {
            pattern: /^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$/,
            message: '输入格式错误，请重新输入!'
          }
        ],
        bankStr: [
          {
            required: true,
            message: '不能为空',
            trigger: 'blur'
          }
        ],
        password: [
          {
            required: true,
            message: '不能为空',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  computed: {
    ...mapGetters([
      'elements',
      'name',
      'merchantId',
      'platformId'
    ])
  },
  methods: {
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },
    handleWithdraw() {
      this.resetForm()
      this.getBankList()
      this.withdrawForm.valAmt = this.valAmt
      this.dialogWithdraw = true
    },
    sureWithdraw(formName) {
      var payAmt = this.withdrawForm.payAmt
      var vAmt = this.withdrawForm.valAmt
      if (payAmt > vAmt) {
        alert('余额不足')
        return
      }
      this.withdrawForm.amount = payAmt.toString()
      this.withdrawForm.bankName = this.withdrawForm.bankStr.split('&')[0]
      this.withdrawForm.bankAccountName = this.withdrawForm.bankStr.split('&')[1]
      this.withdrawForm.bankNo = this.withdrawForm.bankStr.split('&')[2]
      this.withdrawForm.platformId = this.platformId
      this.withdrawForm.merchantId = this.merchantId
      this.withdrawForm.payChannelNo = 'offline001'

      doWithdraw(this.withdrawForm).then(response => {
        this.dialogWithdraw = false
        this.getBalance()
        if (response.code === '0000') {
          this.$notify({
            title: '成功',
            message: '操作成功',
            type: 'success',
            duration: 2000
          })
        } else {
          this.$notify({
            title: '提示',
            message: response.message,
            type: 'error',
            duration: 2000
          })
        }
      })
    },
    dialogAddCallBack() {
      this.dialogWithdraw = false
    },
    getBankList() {
      this.bankQuery.platformId = this.platformId
      this.bankQuery.merchantId = this.merchantId
      pageAll(this.bankQuery).then(response => {
        this.bankList = response.data.rows
      })
    },
    getBalance() {
      var str = {}
      str.platformId = this.platformId
      str.merchantId = this.merchantId
      str.accountType = 'ACC013'
      valiableAmt(str).then(response => {
        this.valAmt = response.balance
      })
    },
    getDataCount() {
      var str = {}
      str.platformId = this.platformId
      str.merchantId = this.merchantId
      dataCount(str).then(response => {
        if (response.data) {
          var ddd = response.data
          if (ddd.count1) {
            this.dataTotal.count1 = ddd.count1
          }
          if (ddd.count2) {
            this.dataTotal.count2 = ddd.count2
          }
          if (ddd.count3) {
            this.dataTotal.count3 = ddd.count3
          }
          if (ddd.count4) {
            this.dataTotal.count4 = ddd.count4
          }
        }
      })
    },
    getList() {
      this.listLoading = true
      this.listQuery.platformId = this.platformId
      if (this.merchantId) {
        this.listQuery.merchantId = this.merchantId
      }
      payForAnotherOrderList(this.listQuery)
        .then(response => {
          console.log(response)
          this.list = response.data.rows
          this.total = response.data.total
          this.listLoading = false
        }).catch(() => {
          this.list = []
          this.total = 0
          this.listLoading = false
        })
    },
    handleUpdate(row) {
      var str = {}
      str.platformId = row.platformId
      str.merchantId = row.merchantId
      str.orderId = row.orderId
      str.payChannelNo = 'offline001'
      str.payCompanyNo = 'OTHER'
      str.type = row.type
      this.$confirm('已提现修改状态, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        updateTxStatus(str).then(() => {
          this.$notify({
            title: '成功',
            message: '操作成功',
            type: 'success',
            duration: 2000
          })
          this.getDataCount()
          this.getList()
        })
      })
    },
    resetForm() {
      this.withdrawForm = {
        platformId: '',
        merchantId: '',
        payAmt: '',
        bankStr: '',
        bankName: '',
        bankAccountName: '',
        bankNo: '',
        valAmt: '',
        payChannelNo: '',
        password: ''
      }
    }
  },
  created() {
    this.getList()
    this.getDataCount()
    if (this.merchantId) {
      this.getBalance()
    }
    this.withdrawManager_btn_upt = this.elements['withdrawManager:btn_upt']
    this.withdrawManager_btn_do = this.elements['withdrawManager:btn_do']
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.panel-group {
  margin-top: 18px;
  .card-panel-col{
    margin-bottom: 32px;
  }
  .card-panel {
    height: 108px;
    cursor: pointer;
    font-size: 12px;
    position: relative;
    overflow: hidden;
    color: #666;
    background: #fff;
    box-shadow: 4px 4px 40px rgba(0, 0, 0, .05);
    border-color: rgba(0, 0, 0, .05);
    &:hover {
      .card-panel-icon-wrapper {
        color: #fff;
      }
      .icon-people {
         background: #40c9c6;
      }
      .icon-message {
        background: #36a3f7;
      }
      .icon-money {
        background: #f4516c;
      }
      .icon-shoppingCard {
        background: #34bfa3
      }
    }
    .icon-people {
      color: #40c9c6;
    }
    .icon-message {
      color: #36a3f7;
    }
    .icon-money {
      color: #f4516c;
    }
    .icon-shoppingCard {
      color: #34bfa3
    }
    .card-panel-icon-wrapper {
      float: left;
      margin: 14px 0 0 14px;
      padding: 16px;
      transition: all 0.38s ease-out;
      border-radius: 6px;
    }
    .card-panel-icon {
      float: left;
      font-size: 48px;
    }
    .card-panel-description {
      float: right;
      font-weight: bold;
      margin-top: 26px;
      margin-left: 0px;
      margin-right: 26px;
      .card-panel-text {
        line-height: 18px;
        color: rgba(0, 0, 0, 0.45);
        font-size: 16px;
        margin-bottom: 12px;
      }
      .card-panel-num {
        font-size: 20px;
      }
    }
    .card-panel-description1 {
      float: left;
      font-weight: bold;
      margin: 26px;
      margin-left: 10px;
    }
  }
}
</style>